package cn.golxen.rsms.aspect;

import cn.golxen.rsms.bean.bo.AddOrOutGoodsNumBo;
import cn.golxen.rsms.bean.entity.GoodsLog;
import cn.golxen.rsms.bean.entity.GoodsPurchase;
import cn.golxen.rsms.bean.entity.GoodsStore;
import cn.golxen.rsms.bean.entity.PayLog;
import cn.golxen.rsms.commons.annotation.AddLog;
import cn.golxen.rsms.commons.utils.LocalDateTimeUtils;
import cn.golxen.rsms.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

@Aspect
@Component
@Slf4j
public class LogAspect {

    @Autowired
    LogService logService;
    @Autowired
    @Qualifier(value = "taskExecutor")
    private ThreadPoolTaskExecutor poolTaskExecutor;

    @Pointcut("@annotation(cn.golxen.rsms.commons.annotation.AddLog)")
    public void LogPointCut(){
    }

    @AfterReturning("LogPointCut()")
    public void saveGoodLog(JoinPoint joinPoint){

        poolTaskExecutor.execute(()->{
//            synchronized (joinPoint){
                //从切面织入点处通过反射机制获取织入点处的方法
                MethodSignature signature = (MethodSignature) joinPoint.getSignature();
                //获取切入点所在的方法
                Method method = signature.getMethod();

                //获取操作
                AddLog addLog = method.getAnnotation(AddLog.class);

                if (addLog !=null){
                    log.info("在 "+signature+" 执行日志通知: "+ addLog.type());
                    String type= addLog.type();
                    if (type.contains("Native下单")||type.contains("收款")){
                        logService.addPayLog((PayLog) joinPoint.getArgs()[0]);
                    }
                    if (type.contains("入库")||type.contains("出库")||type.contains("付款码支付")||type.contains("收款")){
                        if (type.contains("付款码支付")||type.contains("收款"))
                            type="售出";
                        logService.addGoodsLogListByObject(joinPoint.getArgs()[0],type);
                    }


                }

        });

    }
}
